package leetcode.weekly.week294;

import java.util.PriorityQueue;

//Solution2Test
public class Solution2 {

	public int maximumBags(int[] capacity, int[] rocks, int additionalRocks) {
		PriorityQueue<Integer> queue = new PriorityQueue<>((a, b) -> a - b);
		int len = capacity.length;
		int ans = 0;
		long sum = 0;
		for (int i = 0; i < len; i++) {
			int diff = capacity[i] - rocks[i];
			if (diff == 0) {
				ans++;
			} else {
				queue.add(diff);
				sum += diff;
			}
		}
		if (sum <= additionalRocks) {
			return len;
		}
		while (!queue.isEmpty() && additionalRocks > 0) {
			int cur = queue.poll();
			additionalRocks -= cur;
			ans += additionalRocks >= 0 ? 1 : 0;
		}
		return ans;
	}

}
